<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\AddRoleRequest;
use App\Models\Brand;
use App\Models\Commodity;
use App\Models\DealLine;
use App\Models\DealLog;
use App\Models\EDealLog;
use App\Models\LogisticsInfo;
use App\Models\Recharge;
use App\Models\RentCloth;
use App\Models\RentClothAfter;
use App\Models\RentClothDetail;
use App\Models\ShopCom;
use App\Models\ShopHistory;
use App\Models\UserRobe;
use App\Models\Wallet;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

/**
 * @title 线上店铺
 * @description 接口说明
 * @header name:accessToken require:1 default: desc:用户token
 */
class ShopLineController extends ApiController
{

    /**
     * @title 店铺品牌
     * @description 接口说明
     * @author wjx
     * @url /api/shopline/brand_list
     * @method POST
     * @module 店铺相关
     *
     * @param name:shop_id type:int require:1 default: other: desc:商铺ID
     * @return brand_id:品牌ID
     * @return brand_name:品牌名称
     */
    public function brand_list(Request $request)
    {
        $shop_id = $request->shop_id;
        if (!$shop_id) {
            return $this->failed('缺少参数');
        }
        $list = ShopCom::groupBy('brand_id')->where('shop_id', $shop_id)->get('brand_id');

        $list->transform(function ($item) {
            return [
                'brand_id' => $item->brand_id,
                'brand_name' => $item->brand->name,
            ];
        });
        return $this->success($list);
    }
    /**
     * @title 商品搜索页面(2.0)
     * @description 接口说明
     * @author wjx
     * @url /api/shopline/searchGoodsPage
     * @method POST
     * @module 店铺相关
     *
     * @param name:shop_id type:int require:1 default: other: desc:商铺ID
     * @return hot_goods:热搜
     * @return history_goods:历史记录
     */
    public function searchGoodsPage(Request $request)
    {
        $user = $request->user;
        $shop_id = $request->shop_id;
        if (!$shop_id) {
            return $this->failed('参数错误');
        }
        // $hot_goods = ShopCom::where('shop_id', $shop_id)->groupBy('commodity_id')->get()->random(3);
        $hot_goods = ShopHistory::where('shop_id', $shop_id)
            ->selectRaw('COUNT(*) as total_count,commodity_id')
            ->groupBy('commodity_id')
            ->orderBy('total_count', 'desc')
            ->limit(3)
            ->get();

        $hot_goods->transform(function ($item) {
            return [
                commodity::where('id', $item->commodity_id)->value('commodity_name'),
            ];
        });
        $history_goods = ShopHistory::where(['user_id' => $user->id, 'shop_id' => $shop_id])->groupBy('commodity_id')->orderBy('id', 'desc')->limit(10)->get();

        $history_goods->transform(function ($item) {
            return [
                $item->commodity->commodity_name,
            ];
        });
        return $this->success(compact('hot_goods', 'history_goods'));
    }
    /**
     * @title 清空搜索记录(2.0)
     * @description 接口说明
     * @author wjx
     * @url /api/shopline/delSearchLog
     * @method POST
     * @module 店铺相关
     *
     * @param name:shop_id type:int require:1 default: other: desc:商铺ID
     */
    public function delSearchLog(Request $request)
    {
        $user = $request->user;
        $shop_id = $request->shop_id;
        if (!$shop_id) {
            return $this->failed('参数错误');
        }
        $res = ShopHistory::where(['user_id' => $user->id, 'shop_id' => $shop_id])->delete();
        if ($res) {
            return $this->message('清空成功');
        } else {
            return $this->failed('清空失败');
        }
    }
    /**
     * @title 店铺商品搜索(2.0)
     * @description 接口说明
     * @author wjx
     * @url /api/shopline/SearchGoodsButton
     * @method POST
     * @module 店铺相关
     *
     * @param name:shop_id type:int require:1 default: other: desc:商铺ID
     * @param name:keyword type:int require:1 default: other: desc:关键词
     * @return com_id:商品ID
     * @return com_img:商品图片
     * @return com_name:商品名称
     * @return com_desc:商品描述
     * @return is_buy:是否换购0否1是
     * @return com_price:换购价格
     */
    public function SearchGoodsButton(Request $request)
    {
        $user = $request->user;
        $keyword = $request->keyword;
        $shop_id = $request->shop_id;
        if (!$shop_id || !$keyword) {
            return $this->failed('参数错误');
        }

        $lists = ShopCom::where('shop_id', $shop_id)
            ->where(function ($query) use ($keyword) {
                $query->whereHas('commodity', function ($query) use ($keyword) {
                    $query->where('commodity_name', 'like', '%' . $keyword . '%');
                });
            })->groupBy('commodity_id', 'is_buy')
            ->get();
        if (count($lists)) {
            ShopHistory::create([
                'user_id' => $user->id,
                'shop_id' => $shop_id,
                'commodity_id' => $lists->last()->commodity_id,
            ]);
        }

        $lists->transform(function ($item) {
            $item->com_id = $item->commodity_id;
            $item->com_img = config('app.img_url') . ($item->commodity->pictures)[0];
            $item->com_name = $item->commodity->commodity_name;
            $item->com_desc = $item->commodity->commodity_desc;
            $item->com_price = $item->price;
            return $item->only(['com_id', 'com_img', 'com_name', 'com_desc', 'is_buy', 'com_price']);
        });
        return $this->success($lists);
    }

    /**
     * @title 商品列表
     * @description 接口说明
     * @author wjx
     * @url /api/shopline/goods_list
     * @method POST
     * @module 店铺相关
     *
     * @param name:shop_id type:int require:1 default: other: desc:商铺ID
     * @param name:status type:int require: default: other: desc: 状态1上架2待还
     * @param name:brand_id type:int require: default: other: desc: 品牌ID
     * @param name:is_buy type:int require: default: other: desc: 是否换购0否1是
     * @return com_id:商品ID
     * @return com_img:商品图片
     * @return com_name:商品名称
     * @return com_desc:商品描述
     * @return is_buy:是否换购0否1是
     * @return com_price:换购价格
     */
    public function goods_list(Request $request)
    {
        $shop_id = $request->shop_id;
        $status = $request->status;
        $brand_id = $request->brand_id;
        $is_buy = $request->is_buy;
        if (!$shop_id) {
            return $this->failed('缺少参数shop_id');
        }
        $lists = ShopCom::filters([
            ['shop_id', '=', $shop_id],
            ['status', '=', $status],
            ['brand_id', '=', $brand_id],
            ['is_buy', '=', $is_buy],
        ])->with(['commodity'])
            ->groupBy('commodity_id', 'is_buy')
            ->get();

        $lists->transform(function ($item) {
            $item->com_id = $item->commodity_id;
            $item->com_img = config('app.img_url') . ($item->commodity->pictures)[0];
            $item->com_name = $item->commodity->commodity_name;
            $item->com_desc = $item->commodity->commodity_desc;
            $item->com_price = $item->price;
            return $item->only(['com_id', 'com_img', 'com_name', 'com_desc', 'is_buy', 'com_price']);
        });
        return $this->success($lists);
    }

    /**
     * @title 商品详情
     * @description 接口说明
     * @author wjx
     * @url /api/shopline/goods_detail
     * @method POST
     * @module 店铺相关
     *
     * @param name:shop_id type:int require:1 default: other: desc:店铺ID
     * @param name:com_id type:int require:1 default: other: desc:商品ID
     * @param name:is_buy type:int require:1 default: other: desc:能否换购
     * @return com:商品数据
     * @return brand:品牌故事
     */
    public function goods_detail(Request $request)
    {
        $user = $request->user;
        $shop_id = $request->shop_id;
        $com_id = $request->com_id;
        $is_buy = $request->is_buy;
        if (!$shop_id || !$com_id) {
            return $this->failed('缺少参数');
        }
        $lists = ShopCom::where(['shop_id' => $shop_id, 'commodity_id' => $com_id, 'is_buy' => $is_buy])->with(['commodity', 'commodityAttr'])->get();

        if (!$lists) {
            return $this->failed('商品不存在');
        }

        $list = $lists->first();
        $list_attr = $lists->pluck('commodity_attr_id');

        // $attrs = CommodityAttr::whereIn('id',$list_attr)->get(['id','attr_value']);
        $attrs = $lists->map(function ($item) {
            $item->shop_com_id = $item->id;
            $item->attr_value = $item->commodityAttr->attr_value;
            return $item->only(['shop_com_id', 'commodity_attr_id', 'attr_value']);
        });
        $com = [];
        $img_arr = [];
        foreach ($list->commodity->pictures as $value) {
            $img_arr[] = config('app.img_url') . $value;
        }

        $com['commodity_id'] = $list->commodity_id;
        $com['com_img'] = $img_arr;
        $com['com_name'] = $list->commodity->commodity_name;
        $com['com_desc'] = $list->commodity->commodity_desc;
        $com['attr_list'] = $attrs;
        $com['is_buy'] = $list->is_buy;
        $com['price'] = $list->price;

        $brand = Brand::where('id', $list->commodity->brand_id)->value('desc');

        return $this->success(compact('com', 'brand'));
    }
    /**
     * @title 我的衣柜
     * @description 接口说明
     * @author wjx
     * @url /api/shopline/myRobe
     * @method POST
     * @module 衣柜相关
     * @param name:shop_id type:int require:1 default: other: desc:店铺ID
     * @param name:type type:int require:1 default: other: desc:0借衣1换购
     */
    public function myRobe(Request $request)
    {
        $user = $request->user;
        $type = $request->type;
        $shop_id = $request->shop_id;
        if (!isset($type)) {
            return $this->failed('缺少参数');
        }
        if (!$shop_id) {
            return $this->failed('缺少参数');
        }
        $robe_num = $user->robe_num;
        $today_price = DealLine::orderBy('id', 'desc')->value('price');
        $lists = UserRobe::where(['user_id' => $user->id, 'shop_id' => $shop_id, 'type' => $type])->with(['commodity', 'commodityAttr', 'shopcom'])->get();
        $lists->transform(function ($item) use ($today_price) {
            $item->com_img = config('app.img_url') . ($item->commodity->pictures)[0];
            $item->com_name = $item->commodity->commodity_name;
            $item->com_desc = $item->commodity->commodity_desc;
            $item->attr = $item->commodityAttr->attr_value;
            $item->stock = $item->shopcom->stock;
            $item->price = $item->shopcom->price;
            $item->e_price = bcdiv($item->price, $today_price, 4);
            return $item->only(['shop_com_id', 'com_img', 'com_name', 'com_desc', 'commodity_attr_id', 'attr', 'stock', 'price', 'e_price']);
        });

        return $this->success(compact('robe_num', 'lists'));

    }
    /**
     * @title 添加至借衣衣柜
     * @description 接口说明
     * @author wjx
     * @url /api/shopline/addToRobeBorrow
     * @method POST
     * @module 衣柜相关
     *
     * @param name:shop_id type:int require:1 default: other: desc:店铺ID
     * @param name:shop_com_id type:int require:1 default: other: desc:店铺商品ID
     * @param name:commodity_id type:int require:1 default: other: desc:商品ID
     * @param name:commodity_attr_id type:int require:1 default: other: desc:商品属性ID
     */
    public function addToRobeBorrow(Request $request, AddRoleRequest $addrequest, UserRobe $user_robe)
    {
        $user = $request->user;
        $shop_com_id = $request->shop_com_id;
        $is_buy = UserRobe::IS_BORROW;

        $res = ShopCom::where(['id' => $shop_com_id, 'is_buy' => $is_buy])->first();
        if (!$res) {
            return $this->failed('该商品不能借出');
        }
        if ($res->status == 2) {
            return $this->failed('下架商品不能借出');
        }
        $is_has = UserRobe::where([
            'user_id' => $user->id,
            'shop_id' => $request->shop_id,
            'shop_com_id' => $shop_com_id,
            'commodity_id' => $request->commodity_id,
            'commodity_attr_id' => $request->commodity_attr_id,
            'type' => $is_buy,
        ])->count();
        if ($is_has) {
            return $this->failed('同件商品只能借一件');
        }
        $count_num = UserRobe::where(['user_id' => $user->id, 'type' => $is_buy])->count();
        if ($count_num == $user->robe_num) {
            return $this->failed('请充值');
        }
        $request['user_id'] = $user->id;
        $request['type'] = $is_buy;
        $user_robe->fill($request->all());
        $user_robe->save();
        return $this->message('添加成功');
    }
    /**
     * @title 添加至换购衣柜
     * @description 接口说明
     * @author wjx
     * @url /api/shopline/addToRobeBuy
     * @method POST
     * @module 衣柜相关
     *
     * @param name:shop_id type:int require:1 default: other: desc:店铺ID
     * @param name:shop_com_id type:int require:1 default: other: desc:店铺商品ID
     * @param name:commodity_id type:int require:1 default: other: desc:商品ID
     * @param name:commodity_attr_id type:int require:1 default: other: desc:商品属性ID
     */
    public function addToRobeBuy(Request $request, AddRoleRequest $addrequest, UserRobe $user_robe)
    {
        $user = $request->user;
        $shop_com_id = $request->shop_com_id;
        $is_buy = UserRobe::IS_BUY;

        $is_has = UserRobe::where([
            'user_id' => $user->id,
            'shop_id' => $request->shop_id,
            'shop_com_id' => $shop_com_id,
            'commodity_id' => $request->commodity_id,
            'commodity_attr_id' => $request->commodity_attr_id,
            'type' => $is_buy,
        ])->count();

        if ($is_has) {
            return $this->failed('同件商品只能换购一件');
        }
        $res = ShopCom::where(['id' => $shop_com_id, 'is_buy' => $is_buy])->first();
        if (!$res) {
            return $this->failed('该商品不能换购');
        }
        if ($res->status == 2) {
            return $this->failed('下架商品不能换购');
        }
        $request['user_id'] = $user->id;
        $request['type'] = $is_buy;
        $user_robe->fill($request->all());
        $user_robe->save();
        return $this->message('添加成功');
    }
    /**
     * @title 删除衣柜
     * @description 接口说明
     * @author wjx
     * @url /api/shopline/delRobe
     * @method POST
     * @module 衣柜相关
     *
     * @param name:shop_com_id type:int require:1 default: other: desc:店铺商品ID
     * @param name:type type:int require:1 default: other: desc:0借衣1换购
     */
    public function delRobe(Request $request, UserRobe $user_robe)
    {
        $user = $request->user;
        $shop_com_id = $request->shop_com_id;
        $type = $request->type;
        if (!$shop_com_id) {
            return $this->failed('缺少参数');
        }
        if (!$type) {
            $type = 0;
        }
        $user_robe->where(['shop_com_id' => $shop_com_id, 'user_id' => $user->id, 'type' => $type])->delete();
        return $this->message('删除成功');
    }
    /**
     * @title 提交换购订单
     * @description 接口说明
     * @author wjx
     * @url /api/shopline/storeBuyOrder
     * @method POST
     * @module 换购相关
     *
     * @param name:commodity_attr_id type:string require:1 default: other: desc:衣服属性ID(例[1,2,3])
     * @param name:addr_id type:int require:1 default: other: desc:收货地址
     * @param name:shop_id type:int require:1 default: other: desc:店铺id
     * @param name:pay_type type:int require:1 default: other: desc:付款方式(1余额2微信3e格码)
     * @param name:security type:string require: default:1 other: desc:交易密码
     * @param name:total_price type:int require: default:1 other: desc:付款金额(元)
     * @param name:total_e type:int require: default:1 other: desc:付款E格码
     */
    public function storeBuyOrder(Request $request, UserRobe $user_robe, RentCloth $rentcloth)
    {
        $user = $request->user;
        $shop_id = $request->shop_id;
        $pay_type = $request->pay_type;
        $total_price = $request->total_price;

        $total_e = ($request->total_e) ? $request->total_e : 0;
        $addr_id = $request->addr_id;
        $check_pass = Hash::check($request->security, $user->security);

        $attr_id = json_decode($request->commodity_attr_id);

        if (!is_array($attr_id)) {
            return $this->failed('commodity_attr_id参数错误');
        }
        // $total_power = 50;

        if (!$attr_id || !$addr_id || !$shop_id || !$pay_type) {
            return $this->failed('缺少参数');
        }
        // $user_power = $user->wallet->computing_power;
        // if ($user_power < $total_power) {
        //     return $this->failed('算力不足，请充值');
        // }
        // $new_power = bcsub($user_power, $total_power, 2);

        $attr_datas = ShopCom::whereIn('commodity_attr_id', $attr_id)->where(['shop_id' => $shop_id, 'is_buy' => 1])->get();

        if (count($attr_datas) == 0) {
            return $this->failed('没有该商品');
        }
        foreach ($attr_datas as $key => $value) {
            if ($value->stock == 0) {
                return $this->failed('库存不足');
            }
        }

        switch ($pay_type) {
            case '1': //余额支付
                if (!$total_price) {
                    return $this->failed('缺少参数total_price');
                }
                if (!$check_pass) {
                    return $this->failed('交易密码错误');
                }
                if ($user->wallet->money < $total_price) {
                    return $this->failed('余额不足');
                }

                break;
            case '2': //微信支付
                $res = $this->payByWechat($user, $request);
                if ($res) {
                    return $this->success($res);
                } else {
                    return $this->failed('支付失败');
                }
                break;
            case '3': //e支付
                if (!$total_e) {
                    return $this->failed('缺少参数total_e');
                }
                if (!$check_pass) {
                    return $this->failed('交易密码错误');
                }
                if ($user->wallet->tmc < $total_e) {
                    return $this->failed('余额不足');
                }
                break;
            default:
                break;
        }

        $rent_data = [
            'user_id' => $user->id,
            'shop_id' => $shop_id,
            'type' => 3,
            'addr_id' => $addr_id,
            'pay_type' => $pay_type,
            'pay_at' => Carbon::now(),
            'total_price' => $total_price,
            'total_e' => $total_e,
            'status' => 3,
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now(),
        ];
        $wallet = $user->wallet;
        DB::beginTransaction();
        try {
            $rent_id = RentCloth::insertGetId($rent_data);
            UserRobe::where(['user_id' => $user->id, 'type' => 1])->delete();
            foreach ($attr_datas as $key => $value) {
                RentClothDetail::insert([
                    'user_id' => $user->id,
                    'rent_id' => $rent_id,
                    'commodity_id' => $value->commodity_id,
                    'commodity_attr_id' => $value->commodity_attr_id,
                    'created_at' => Carbon::now(),
                    'updated_at' => Carbon::now(),
                ]);
                ShopCom::where(['shop_id' => $value->shop_id, 'commodity_attr_id' => $value->commodity_attr_id, 'is_buy' => 1])->decrement('stock', 1); //目前每件衣服只能借一件
            }

            // Wallet::where('user_id', $user->id)->update([
            //     'computing_power' => $new_power,
            // ]);
            // PowerLog::add_log($user->id, 9, $total_power, 2);
            switch ($pay_type) {
                case '1': //余额
                    $wallet->money = bcsub($wallet->money, $total_price, 2);
                    $wallet->save();
                    DealLog::add_log($user->id, 0, $total_price, 2, '换购消耗余额');
                    break;
                case '3': //E格码
                    $wallet->tmc = bcsub($wallet->tmc, $total_e, 2);
                    $wallet->save();
                    EDealLog::add_log($user->id, 0, $total_e, 2, '换购消耗E格码', 0);
                    break;
                default:
                    break;
            }

            DB::commit();
            return $this->message('交易成功');
        } catch (\Exception $exception) {

            DB::rollBack();
            return $this->failed('交易失败');
        }
    }

    public function payByWechat($user, $request)
    {
        if (!$request->total_price) {
            return $this->failed('缺少参数total_price');
        }

        $attr_id = json_decode($request->commodity_attr_id);
        $attr_datas = ShopCom::whereIn('commodity_attr_id', $attr_id)->where(['shop_id' => $request->shop_id, 'is_buy' => 1])->get();
        $rent = new RentCloth();
        $rent->user_id = $user->id;
        $rent->shop_id = $request->shop_id;
        $rent->type = 3;
        $rent->pay_type = 2;
        $rent->addr_id = $request->addr_id;
        $rent->attr_list = $request->commodity_attr_id;
        $rent->out_trade_no = set_order_no();
        $rent->total_price = $request->total_price;
        $rent->status = 8; //微信未支付

        $recharge = new Recharge();
        $recharge->user_id = $user->id;
        $recharge->money = $request->total_price;
        $recharge->type = 7; //充值换购订单
        $recharge->pay_type = 1; //默认微信
        $recharge->status = 1; //未成功付款
        $recharge->out_trade_no = $rent->out_trade_no;
        DB::beginTransaction();
        try {
            $rent->save();
            $recharge->save();
            UserRobe::where(['user_id' => $user->id, 'type' => 2])->delete();
            foreach ($attr_datas as $key => $value) {
                RentClothDetail::insert([
                    'user_id' => $user->id,
                    'rent_id' => $rent->id,
                    'commodity_id' => $value->commodity_id,
                    'commodity_attr_id' => $value->commodity_attr_id,
                    'created_at' => Carbon::now(),
                    'updated_at' => Carbon::now(),
                ]);
                ShopCom::where(['shop_id' => $value->shop_id, 'commodity_attr_id' => $value->commodity_attr_id])->decrement('stock', 1); //目前每件衣服只能借一件
            }
            DB::commit();

            $pay = app('wechat_pay')->app([
                'out_trade_no' => $recharge->out_trade_no, // 商户订单流水号，与支付宝 out_trade_no 一样
                // 'total_fee' => $total_price * 100, // 与支付宝不同，微信支付的金额单位是分。
                'total_fee' => 1, // 与支付宝不同，微信支付的金额单位是分。
                'body' => '换购商品', // 订单描述
            ]);
            return json_decode($pay->getContent());

        } catch (\Exception $exception) {
            Log::info('wechat error7', [
                'error' => $exception->getMessage(),
            ]);
            DB::rollBack();
            return false;
        }

    }

    /**
     * @title 借衣订单
     * @description 接口说明:2已归还4线上已发货(待签收)5线上已收货(待归还) 不传为全部,logistics_info.type:1线上借衣2线上还衣3线上换购4线上退货
     * @author wjx
     * @url /api/shopline/myOrderLineBorrow
     * @method POST
     * @module 线上订单
     * @param name:status type:int require:1 default: other: desc:4线上已发货(待签收)5线上已收货(待归还)6已归还
     * @param name:type type:string require:1 default:refresh other: desc:方法参数:more/refresh
     * @param name:offset type:int require:1 default:10 other: desc:方法参数:每页数据量
     * @param name:ID type:int require:1 default:-1 other: desc:数据起点ID
     */
    public function myOrderLineBorrow(Request $request)
    {
        $user = $request->user;
        $type = 1;
        $status = $request->status ? $request->status : 0;
        $startId = $request->ID ? $request->ID : 0;
        $offset = $request->offset ? $request->offset : 10;
        if ($offset > 25) {
            $offset = 25;
        }
        $loadType = $request->type;
        if (!in_array($loadType, ['more', 'refresh'])) {
            return $this->failed('type参数错误');
        }
        if ($loadType == 'more') {
            $rent_cloths = RentCloth::filters([
                ['user_id', '=', $user->id],
                ['type', '=', $type],
                ['status', '=', $status],
                ['id', '<', $startId],
            ])
                ->where('status', '<>', 8)
                ->take($offset)
                ->with(['rentclothdetail', 'logistics'])
                ->orderBy('id', 'desc')
                ->get();
        } else {
            $rent_cloths = RentCloth::filters([
                ['user_id', '=', $user->id],
                ['type', '=', $type],
                ['status', '=', $status],
                ['id', '>', $startId],
            ])
                ->where('status', '<>', 8)
                ->take($offset)
                ->with(['rentclothdetail', 'logistics'])
                ->orderBy('id', 'desc')
                ->get();
        }

        foreach ($rent_cloths as $key => $value) {
            $rent_cloths[$key] = $value->transform_lineBorrow_list();
        }
        return $this->success($rent_cloths);
    }

    /**
     * @title 换购订单
     * @description 接口说明:3待发货4已发货5已收货logistics_info.type:1线上借衣2线上还衣3线上退货
     * @author wjx
     * @url /api/shopline/myOrderLineBuy
     * @method POST
     * @module 线上订单
     * @param name:status type:int require:1 default: other: desc:3待发货4已发货5已收货
     * @param name:type type:string require:1 default:refresh other: desc:方法参数:more/refresh
     * @param name:offset type:int require:1 default:10 other: desc:方法参数:每页数据量
     * @param name:ID type:int require:1 default:-1 other: desc:数据起点ID
     */
    public function myOrderLineBuy(Request $request)
    {
        $user = $request->user;
        $type = 3; //换购
        $status = $request->status ? $request->status : 0;

        $startId = $request->ID ? $request->ID : 0;
        $offset = $request->offset ? $request->offset : 10;
        if ($offset > 25) {
            $offset = 25;
        }
        $loadType = $request->type;
        if (!in_array($loadType, ['more', 'refresh'])) {
            return $this->failed('type参数错误');
        }
        if ($loadType == 'more') {
            $rent_cloths = RentCloth::filters([
                ['user_id', '=', $user->id],
                ['type', '=', $type],
                ['status', '=', $status],
                ['id', '<', $startId],
            ])
                ->where('status', '<>', 8)
                ->take($offset)
                ->with(['rentclothdetail', 'logistics'])
                ->orderBy('id', 'desc')
                ->get();
        } else {
            $rent_cloths = RentCloth::filters([
                ['user_id', '=', $user->id],
                ['type', '=', $type],
                ['status', '=', $status],
                ['id', '>', $startId],
            ])
                ->where('status', '<>', 8)
                ->take($offset)
                ->with(['rentclothdetail', 'logistics'])
                ->orderBy('id', 'desc')
                ->get();
        }

        foreach ($rent_cloths as $key => $value) {
            $rent_cloths[$key] = $value->transform_lineBuy_list();
        }
        return $this->success($rent_cloths);
    }
    /**
     * @title 换购售后订单
     * @description 接口说明:status:11退货中12审核中13已退款
     * @author wjx
     * @url /api/shopline/myOrderLineBuyAfter
     * @method POST
     * @module 线上订单
     * @param name:type type:string require:1 default:refresh other: desc:方法参数:more/refresh
     * @param name:offset type:int require:1 default:10 other: desc:方法参数:每页数据量
     * @param name:ID type:int require:1 default:-1 other: desc:数据起点ID
     */
    public function myOrderLineBuyAfter(Request $request)
    {
        $user = $request->user;
        $status = $request->status ? $request->status : 0;

        $startId = $request->ID ? $request->ID : 0;
        $offset = $request->offset ? $request->offset : 10;
        if ($offset > 25) {
            $offset = 25;
        }
        $loadType = $request->type;
        if (!in_array($loadType, ['more', 'refresh'])) {
            return $this->failed('type参数错误');
        }
        if ($loadType == 'more') {
            $rent_cloths = RentClothAfter::filters([
                ['user_id', '=', $user->id],
                ['id', '<', $startId],
            ])
                ->where('status', '<>', 8)
                ->take($offset)
                ->orderBy('id', 'desc')
                ->get();
        } else {
            $rent_cloths = RentClothAfter::filters([
                ['user_id', '=', $user->id],
                ['id', '>', $startId],
            ])
                ->take($offset)
                ->orderBy('id', 'desc')
                ->get();
        }

        foreach ($rent_cloths as $key => $value) {
            $rent_cloths[$key] = $value->transform_buyAfter_list();
        }
        return $this->success($rent_cloths);
    }

    /**
     * @title 订单详情
     * @description 接口说明:2已归还3线上待发货4线上已发货5线上已收货6线上已归还7售后;pay_type:1余额2微信3E格码
     * @author wjx
     * @url /api/shopline/orderLinedetail
     * @method POST
     * @module 线上订单
     * @param name:rent_id type:int require:1 default: other: desc:记录ID
     */
    public function orderLinedetail(Request $request)
    {
        $user = $request->user;
        $rent_id = $request->rent_id;

        $rent_cloths = RentCloth::where('id', $rent_id)->with(['rentclothdetail', 'logistics'])->first();

        $rent_cloths = $rent_cloths->transform_lineDetail();

        return $this->success($rent_cloths);
    }
    /**
     * @title 确认收货
     * @description 接口说明:
     * @author wjx
     * @url /api/shopline/confirmOrder
     * @method POST
     * @module 线上订单
     * @param name:rent_id type:int require:1 default: other: desc:记录ID
     */
    public function confirmOrder(Request $request)
    {
        $user = $request->user;
        $rent_id = $request->rent_id;

        $rent_cloths = RentCloth::where('id', $rent_id)->first();
        $logistic = LogisticsInfo::where(['rent_id' => $rent_id, 'type' => 1])->first();

        if ($rent_cloths->status !== 4) {
            return $this->failed('订单状态错误');
        }
        $rent_cloths->status = 5;
        $logistic->receive_time = time();
        $logistic->save();
        if ($rent_cloths->save()) {
            return $this->message('收货成功');
        } else {
            return $this->failed('收货失败');
        }
    }
    /**
     * @title 申请还衣(借还)
     * @description 接口说明:
     * @author wjx
     * @url /api/shopline/replyReturnOrder
     * @method POST
     * @module 线上订单
     * @param name:rent_id type:int require:1 default: other: desc:记录ID
     */
    public function replyReturnOrder(Request $request)
    {
        $user = $request->user;
        $rent_id = $request->rent_id;

        $rent_cloths = RentCloth::where('id', $rent_id)->first();

        if ($rent_cloths->status !== 5) {
            return $this->failed('订单状态错误');
        }
        $rent_cloths->is_apply = 1;

        if ($rent_cloths->save()) {
            return $this->message('申请成功');
        } else {
            return $this->failed('申请失败');
        }
    }

    /**
     * @title 申请退换(换购)
     * @description 接口说明:
     * @author wjx
     * @url /api/shopline/replyAfterOrder
     * @method POST
     * @module 线上订单
     * @param name:rent_id type:int require:1 default: other: desc:记录ID
     * @param name:commodity_attr_id type:string require:1 default: other: desc:商品属性id(例[1,2,3])
     * @param name:comment type:string require:1 default: other: desc:备注
     */
    public function replyAfterOrder(Request $request)
    {
        $user = $request->user;
        $rent_id = $request->rent_id;
        $attr_id = $request->commodity_attr_id;
        $comment = $request->comment;
        if (!is_array(json_decode($attr_id))) {
            return $this->failed('参数格式错误');
        }
        if (!$comment) {
            return $this->failed('请输入备注信息');
        }
        $rent_cloths = RentCloth::where('id', $rent_id)->first();
        if ($rent_cloths->status !== 5) {
            return $this->failed('订单状态错误');
        }
        $is_has = RentClothAfter::where('rent_id', $rent_id)->first();
        if ($is_has) {
            return $this->failed('该订单已申请退换,请耐心等待');
        }
        $rent_cloths->is_apply = 1;
        // $rent_cloths->status = 7;//售后
        $price = ShopCom::where(['shop_id' => $rent_cloths->shop_id, 'is_buy' => 1])->whereIn('commodity_attr_id', json_decode($attr_id))->sum('price');

        $rentafter = new RentClothAfter();
        $rentafter->user_id = $user->id;
        $rentafter->rent_id = $rent_id;
        $rentafter->shop_id = $rent_cloths->shop_id;
        $rentafter->commodity_attr_id = $attr_id;
        $rentafter->price = $price;
        $rentafter->comment = $comment;
        $rentafter->status = 11;
        DB::beginTransaction();
        try {
            $rentafter->save();
            $rent_cloths->save();
            DB::commit();
            return $this->message('申请成功');
        } catch (\Exception $exception) {
            DB::rollBack();
            return $this->failed('申请失败');
        }

    }

}
